package com.sinaapp.aidaka.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.sinaapp.aidaka.server.domain.User;

@Repository
public class UserDao {
	
	private static final String USER_TABLE_NAME = "t_user";

	@Autowired
	private JdbcTemplate jdbcTemplate ;
	
	/**
	 * 通过用户的email 查找是否有该用户
	 * @param email
	 * @return
	 */
	public boolean isUserExists(String email) {
		String sql = "select count(u_id) from " + USER_TABLE_NAME + 
					 " where u_email = ?";
		int count = jdbcTemplate.queryForInt(sql, new Object[]{email});
		if(count > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 验证邮箱和密码是否匹配
	 * @param email
	 * @param password
	 * @return
	 */
	public boolean isEmailAndPasswordMatch(String email, String password){
		String sql = "select count(u_id) from " + USER_TABLE_NAME + 
					 " where u_email = ? and u_passwd = ?";
		int count = jdbcTemplate.queryForInt(sql, new Object[]{email, password});
		if(count > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断用户的密码是否为空
	 * @param email
	 * @return
	 */
	public boolean isUserPasswordEmpty(String email) {
		String sql = "select u_passwd from " + USER_TABLE_NAME + 
					 " where u_email = ?";
		final String[] passwords = new String[]{""};
		jdbcTemplate.query(sql, new Object[]{email}, new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				passwords[0] = rs.getString("u_passwd");
			}
		});
		
		return StringUtils.isBlank(passwords[0]);
	}
	
	/**
	 * 增加新的用户
	 * 只需要用户的email 和密码 密码可以为“”
	 */
	public void saveNewUser(String email, String password, Date lastLogin, String lastIp) {
		String sql = "insert " + USER_TABLE_NAME + 
				 	 " (u_email, u_passwd, last_login, last_ip) values (?, ?, ?, ?)";
		jdbcTemplate.update(sql, new Object[]{email, password, lastLogin, lastIp});
	}
	
	/**
	 * 更新用户的邮箱和密码
	 * @param user
	 */
	public void updateUser(User user) {
		String sql = "update " + USER_TABLE_NAME + 
					 " set u_email = ? , u_passwd = ? where u_id = ?";
		jdbcTemplate.update(sql, new Object[]{user.getEmail(), user.getPassword(), user.getUid()});
	}
	
	/**
	 * 根据email 获得用户基础信息
	 * @param email
	 * @return 无对应email用户信息则返回null
	 */
	public User findUserByEmail(final String email) {
		String sql = "select u_id, u_passwd, is_activate, last_login, last_ip, u_statu from " +
					 USER_TABLE_NAME + " where u_email = ?";
		final User user = new User();
		jdbcTemplate.query(sql, new Object[]{email}, new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				user.setUid(rs.getLong("u_id"));
				user.setEmail(email);
				user.setPassword(rs.getString("u_passwd"));
				user.setActicate(rs.getBoolean("is_activate"));
				user.setLastLoginTime(rs.getTimestamp("last_login"));
				user.setLastIP(rs.getString("last_ip"));
				user.setuStatu(rs.getInt("u_statu"));
			}
		});
		if(user.getUid() == 0) {
			return null;
		}
		return user;
	}
	
	/**
	 * 更新用户的登录状态
	 * 
	 */
	public void updateUserLoginData(long uId, Date lastLoginTime, String lastIP) {
		String sql = "update " + USER_TABLE_NAME + 
			 	 	 " set last_login = ?, last_ip = ? where u_id = ?";
		jdbcTemplate.update(sql, new Object[]{lastLoginTime, lastIP, uId});
	}
	
	/**
	 * 设置用户已激活
	 * @param uId
	 */
	public void setActivate(long uId) {
		String sql = "update " + USER_TABLE_NAME + 
				 	 " set is_activate = 1 where u_id = ?";
		jdbcTemplate.update(sql, new Object[]{uId});
	}
	
	/**
	 * 标记该用户为已删除用户
	 * @param uId
	 * @param isDelete
	 */
	public void updateIsDelete(long uId, int isDelete) {
		String sql = "update " + USER_TABLE_NAME + 
			 	 	 " set is_delete = ? where u_id = ?";
		jdbcTemplate.update(sql, new Object[]{isDelete, uId});
	}
	
	/**
	 * 删除指定user
	 * @param uId
	 */
	public void deleteUser(long uId) {
		String sql = "delete from " + USER_TABLE_NAME + 
					 " where u_id = ?";
		jdbcTemplate.update(sql, new Object[]{uId});
	}
	
	/**
	 * 通过邮箱修改密码
	 * @param email
	 * @param password
	 */
	public void changePassword(String email, String password) {
		String sql = "update " + USER_TABLE_NAME + 
		 	 	 	 " set u_passwd = ? where u_email = ?";
		jdbcTemplate.update(sql, new Object[]{password, email});
	}
}
